package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.StudentSubject;
import com.ruoyi.system.mapper.ExamMapper;
import com.ruoyi.system.mapper.PaperSubjectMapper;
import com.ruoyi.system.mapper.StudentSubjectMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PaperSubject;
import com.ruoyi.system.service.IPaperSubjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷考题表Controller
 *
 * @author ruoyi
 * @date 2022-11-20
 */
@RestController
@RequestMapping("/system/subject/paper")
public class PaperSubjectController extends BaseController
{
    @Autowired
    private IPaperSubjectService paperSubjectService;

    @Autowired
    private StudentSubjectMapper studentSubjectMapper;

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private PaperSubjectMapper paperSubjectMapper;


    @GetMapping("/status/{studentId}/{examId}")
    public TableDataInfo status(@PathVariable Long studentId,@PathVariable Long examId)
    {
        startPage();
        StudentSubject studentSubject=new  StudentSubject();
        studentSubject.setStudentId(studentId);
        studentSubject.setExamId(examId);
        List<StudentSubject> studentSubjects = studentSubjectMapper.selectStudentSubjectList(studentSubject);
        Long paperId = examMapper.selectExamByExamId(examId).getPaperId();
        for (int i = 0; i < studentSubjects.size(); i++) {
            PaperSubject paperSubject= new PaperSubject();
            paperSubject.setSubjectId(studentSubjects.get(i).getSubjectId());
            paperSubject.setPaperId(paperId);
            List<PaperSubject> paperSubjectList = paperSubjectService.selectPaperSubjectList(paperSubject);
            String subjectNow = paperSubjectList.get(0).getSubjectNow();
            studentSubjects.get(i).setSubjectId(Long.valueOf(subjectNow));
        }
        return getDataTable(studentSubjects);
    }
    /**
     * 查询试卷考题表列表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:list')")
    @GetMapping("/list")
    public TableDataInfo list(PaperSubject paperSubject)
    {
        startPage();
        List<PaperSubject> list = paperSubjectService.selectPaperSubjectList(paperSubject);
        return getDataTable(list);
    }

    /**
     * 导出试卷考题表列表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:export')")
    @Log(title = "试卷考题表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PaperSubject paperSubject)
    {
        List<PaperSubject> list = paperSubjectService.selectPaperSubjectList(paperSubject);
        ExcelUtil<PaperSubject> util = new ExcelUtil<PaperSubject>(PaperSubject.class);
        util.exportExcel(response, list, "试卷考题表数据");
    }

    /**
     * 获取试卷考题表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:subject:query')")
    @GetMapping(value = "/{paperId}")
    public AjaxResult getInfo(@PathVariable("paperId") Long paperId)
    {
        return success(paperSubjectService.selectPaperSubjectByPaperId(paperId));
    }

    /**
     * 新增试卷考题表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:add')")
    @Log(title = "试卷考题表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PaperSubject paperSubject)
    {
        return toAjax(paperSubjectService.insertPaperSubject(paperSubject));
    }

    /**
     * 修改试卷考题表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:edit')")
    @Log(title = "试卷考题表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PaperSubject paperSubject)
    {
        return toAjax(paperSubjectService.updatePaperSubject(paperSubject));
    }

    /**
     * 删除试卷考题表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:remove')")
    @Log(title = "试卷考题表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paperIds}")
    public AjaxResult remove(@PathVariable Long[] paperIds)
    {
        return toAjax(paperSubjectService.deletePaperSubjectByPaperIds(paperIds));
    }
}
